package cd.safe.school.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cd.safe.school.R;
import cd.safe.school.activity.EnglishEditDetailActivity;
import cd.safe.school.bean.SharePager;
import cd.safe.school.data.AppData;
import cd.safe.school.util.DateUtil;
import cd.safe.school.util.DownloadUpload;
import cd.safe.school.util.XmlUtil;
import cd.safe.school.util.ZipUtil;

public class ErrorLibraryAdapter extends BaseAdapter {
	private List<SharePager> lists;
	private Context context;
	public static Handler handler;
	private AppData ad;
	private SQLiteDatabase db;
	private String path;
	private String coursepath;

	public ErrorLibraryAdapter(Context context, List<SharePager> lists) {
		this.context = context;
		this.lists = lists;
		ad = new AppData(context);
		db = ad.getWritableDatabase();

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return lists.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		if (convertView == null) {

			LayoutInflater in = LayoutInflater.from(context);
			convertView = in.inflate(R.layout.english_edit_item, null);
		}
		final TextView english_name = (TextView) convertView
				.findViewById(R.id.engilsh_name);
		ImageView english_edit = (ImageView) convertView
				.findViewById(R.id.english_edit);

		english_name.setText(lists.get(position).getTitle());

		english_edit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Cursor c = db.query("Courseware", null, "courseID=?",
						new String[] { String.valueOf(lists.get(position)
								.getId()) }, null, null, null);
				if (c.moveToNext()) {
					coursepath = c.getString(c.getColumnIndex("path"));
					if (coursepath != null && !"".equals(coursepath)) {
						File file = new File(coursepath);
						if (!file.exists()) {
							coursepath = Environment
									.getExternalStorageDirectory()
									+ "/safeschool/error/"
									+ lists.get(position).getId()
									+ "/kejian.zip";
							ContentValues values = new ContentValues();
							values.put("path", coursepath);

							db.update(
									"Courseware",
									values,
									"courseID=?",
									new String[] { String.valueOf(lists.get(
											position).getId()) });

							DownloadUpload.downloadefile(lists.get(position)
									.getUrl(), coursepath);
						}
					} else {
						coursepath = Environment.getExternalStorageDirectory()
								+ "/safeschool/error/"
								+ lists.get(position).getId() + "/kejian.zip";
						ContentValues values = new ContentValues();
						values.put("path", coursepath);

						db.update(
								"Courseware",
								values,
								"courseID=?",
								new String[] { String.valueOf(lists.get(
										position).getId()) });
						DownloadUpload.downloadefile(lists.get(position)
								.getUrl(), coursepath);

					}

				} else {
					coursepath = Environment.getExternalStorageDirectory()
							+ "/safeschool/error/"
							+ lists.get(position).getId() + "/kejian.zip";

					ContentValues values = new ContentValues();
					values.put("path", coursepath);
					values.put("courseID", lists.get(position).getId());
					values.put("createTime", DateUtil.getCurrentDate());
					values.put("type", lists.get(position).getType());
					db.insert("Courseware", "courseID", values);

					DownloadUpload.downloadefile(lists.get(position).getUrl(),
							coursepath);
				}

				try {

					ZipUtil.upZipSelectedFile(new File(coursepath),
							Environment.getExternalStorageDirectory()
									+ "/safeschool/error/"
									+ lists.get(position).getId());

					XmlUtil.parsexml(Environment.getExternalStorageDirectory()
							+ "/safeschool/error/"
							+ lists.get(position).getId() + "/GZ.xml");
					// music_path = Environment.getExternalStorageDirectory()
					// + "/safeschool/course/" +
					// lists.get(position).get("courseid")
					// + "/" + XmlUtil.musicname;
					path = Environment.getExternalStorageDirectory()
							+ "/safeschool/error/"
							+ lists.get(position).getId();

				} catch (Exception e) {
					e.printStackTrace();
					handler.sendEmptyMessage(1001);
				}

				Message msg = new Message();
				msg.what = 4;
				Bundle bun = msg.getData();
				bun.putString("path", path);
				bun.putString("tag", "error");

				handler.sendMessage(msg);
			}
		});
		return convertView;
	}
}
